import moment from "moment";
import "moment-timezone";
// const main = JSON.parse(sessionStorage.getItem("timeZone"));


// 时间戳转换（不带时区）
export function dateFormat(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return "-";
  }
  const format = cFormat || "{y}-{m}-{d}"; /* '{y}-{m}-{d} {h}:{i}:{s}' */
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };

  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return timeStr;
}

// 时间戳转换（带时区）
export function dateTimeZone(
  time,
  cFormat = "{y}-{m}-{d} {h}:{i}:{s}",
  timeZone = "UTC"
) {
  if (arguments.length === 0 || !time) {
    return "-";
  }
  const format = cFormat || "{y}-{m}-{d}"; /* '{y}-{m}-{d} {h}:{i}:{s}' */

  let date;
  if (typeof time === "object") {
    date = moment(time);
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = moment(time);
  }

  date = date.tz(timeZone);
  const formatObj = {
    y: date?.year(),
    m: date?.month() + 1,
    d: date?.date(),
    h: date?.hour(),
    i: date?.minute(),
    s: date?.second(),
    a: date?.day()
  };

  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  
  return timeStr;
}

export function getThreeDaysAgo(value) {
  let today = new Date();
  let threeDaysAgo = new Date(today.getTime() - value * 24 * 60 * 60 * 1000);
  let year = threeDaysAgo.getFullYear();
  let month = String(threeDaysAgo.getMonth() + 1).padStart(2, "0");
  let day = String(threeDaysAgo.getDate()).padStart(2, "0");
  return year + "-" + month + "-" + day;
}

//几月前
export function getmonth(value) {
  let d = new Date();
  d.setMonth(d.getMonth() - value);
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var day = d.getDate();
  var formattedDate =
    year +
    "-" +
    (month < 10 ? "0" + month : month) +
    "-" +
    (day < 10 ? "0" + day : day);
  return formattedDate;
}

//几小时前的年月日
export function datetimes() {
  let now = new Date();
  now.setHours(now.getHours()); //- 12

  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let day = now.getDate();
  let hours = now.getHours();
  let minutes = now.getMinutes();
  let seconds = now.getSeconds();

  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  if (hours < 10) {
    hours = "0" + hours;
  }
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  if (seconds < 10) {
    seconds = "0" + seconds;
  }

  let dateTime = year + "-" + month + "-" + day;
  return dateTime;
}

//几小时前是周几
export function getDayOfWeek12HoursAgo(val) {
  if (val) {
    let now = val;
    now.setHours(now.getHours()); // - 12
    let dayOfWeek = now.getDay();
    let weekdays = ["7", "1", "2", "3", "4", "5", "6"];
    return weekdays[dayOfWeek];
  }
}
